package kali.wpj.kali.web;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.marshalchen.common.commonUtils.urlUtils.HttpUtilsAsync;
import com.marshalchen.common.ui.ToastUtil;
import com.marshalchen.common.uimodule.androidanimations.Techniques;
import com.marshalchen.common.uimodule.androidanimations.YoYo;

import org.apache.http.Header;

import java.io.UnsupportedEncodingException;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import kali.wpj.kali.MainActivity;
import kali.wpj.kali.R;
import kali.wpj.kali.common.CircleProgress;
import kali.wpj.kali.common.CustomProgressDialog;
import kali.wpj.kali.common.settingActivity;
import kali.wpj.kali.globe.ServiceInfo;
import kali.wpj.kali.util.DialogBuilderTools;
import kali.wpj.kali.util.IPTools;
import kali.wpj.kali.util.SharedTools;


public class WebEvaluationActivity extends Activity {
    private String PARENT_URL = ServiceInfo.NIKTO;
    @InjectView(R.id.ed_webEvaUrl)
    EditText webEvaUrl;
    @InjectView(R.id.btn_webEvaRe)
    Button webEvaReset;
    @InjectView(R.id.btn_webEvaStart)
    Button webEvaStart;
    @InjectView(R.id.btn_look)
    Button btn_look;
    private DialogBuilderTools dailog;
    private String IP;
    @InjectView(R.id.progress)
    CircleProgress mProgressView;
    @InjectView(R.id.txt_evalution)
    TextView txt_evalution;
    private String name;
    private YoYo.YoYoString rope;
    private View mTarget;
//    private Dialog mDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_evaluation);
        ButterKnife.inject(this);
        mTarget=findViewById(R.id.txt_evalution);
        rope=YoYo.with(Techniques.RollIn).duration(2000).playOn(mTarget);
        mProgressView = (CircleProgress) findViewById(R.id.progress);
//        if (!IPTools.haveHostIP(WebEvaluationActivity.this)) {
//            Toast.makeText(WebEvaluationActivity.this, getString(R.string.setIP), Toast.LENGTH_SHORT).show();
//            startActivity(new Intent(WebEvaluationActivity.this, settingActivity.class));
//            overridePendingTransition(R.anim.ac_transition_unzoom_in, R.anim.ac_transition_unzoom_out);
//        } else {

            IP = ServiceInfo.IP;
       // }
        btn_look.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
      System.out.println("------>" + ServiceInfo.serviceIP + "/report" + name);
                Intent intent = new Intent(WebEvaluationActivity.this, OnlineSeeActivity.class);
               intent.putExtra("url", "http://"+ServiceInfo.serviceIP + "/report" + name);
                startActivity(intent);
         overridePendingTransition(R.anim.material_design_snackbar_hide_animation, R.anim.material_design_snackbar_show_animation);
            }
        });
    }
    @OnClick(R.id.txt_evalution)
    public void TextViewClick(){
        if(rope!=null){
            rope.stop(true);
        }
    }


    @OnClick(R.id.btn_webEvaRe)
    public void webReste() {
        webEvaUrl.setText("");
    }

    @OnClick(R.id.btn_webEvaStart)
    public void StartNikto() {

        String ip = webEvaUrl.getText().toString().trim().replaceAll(" ", "");
        System.out.println("start----------------" + ip);
        if (IPTools.isIPAddress(ip) || IPTools.isWeb(ip)) {
            getResult(IP, ip);
        } else {
            dailog = new DialogBuilderTools(WebEvaluationActivity.this);
            dailog.dialogShow(getString(R.string.IPOrWebError));
            return;
        }


    }

    /**
     *
     *
     * @param Ip server ip
     * @param ip  web ip
     */
    private void getResult(String Ip, String ip) {
        String tem="http://" + Ip + PARENT_URL + ip + "/";
        HttpUtilsAsync.setTimeout(9999999);
        Log.i("----------",tem);
        Log.i("----------", HttpUtilsAsync.getTimeout() + "");
        HttpUtilsAsync.get(tem, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                System.out.println(getString(R.string.ok) + bytes.toString());

                try {
                    name = ((new String(bytes, "UTF-8").replace("\"", "")));

                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                System.out.println("return values:" + name);

                btn_look.setVisibility(View.VISIBLE);
            }


            @Override
            public void onFinish() {
                mProgressView.stopAnim();
                mProgressView.setVisibility(View.GONE);
                visi();

            }

            @Override
            public void onStart() {
              mProgressView.setVisibility(View.VISIBLE);
                gone();
               // showRoundProcessDialog(WebEvaluationActivity.this,R.layout.process_dialog_anim);
               mProgressView.startAnim();
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
              //  mDialog.dismiss();
                ToastUtil.show(WebEvaluationActivity.this, getString(R.string.web));

            }
        });


    }

    @Override
    public void onBackPressed() {
        startActivity(new Intent(WebEvaluationActivity.this, MainActivity.class));
        overridePendingTransition(R.anim.ac_transition_flip_horizontal_in, R.anim.ac_transition_flip_horizontal_out);
    }
    public void gone(){
        webEvaUrl.setVisibility(View.GONE);
        webEvaReset.setVisibility(View.GONE);
        webEvaStart.setVisibility(View.GONE);
        btn_look.setVisibility(View.GONE);
    }
    public void visi(){
        webEvaUrl.setVisibility(View.VISIBLE);
        webEvaReset.setVisibility(View.VISIBLE);
        webEvaStart.setVisibility(View.VISIBLE);
        btn_look.setVisibility(View.VISIBLE);
    }

//    public void showRoundProcessDialog(Context mContext, int layout) {
//        mDialog = new AlertDialog.Builder(mContext).create();
//        mDialog.setTitle(getString(R.string.coffee));
//        mDialog.show();
//        // 注意此处要放在show之后 否则会报异常
//        mDialog.setContentView(layout);
//    }


}
